package cn.harry.wx.entity;

import cn.hutool.core.date.DatePattern;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户表
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Data
@TableName("wx_user")
public class WxUser implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @ApiModelProperty(value = "ID")
    private Long id;

    /**
     * 微信openid
     */
    @TableId(type = IdType.INPUT)
    @ApiModelProperty(value = "微信openid")
    private String openid;
    /**
     * appid
     */
    @ApiModelProperty(value = "appid")
    private String appid;
    /**
     * 手机号
     */
    @ApiModelProperty(value = "手机号")
    private String phone;
    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称")
    private String nickname;
    /**
     * 性别(0-未知、1-男、2-女)
     */
    @ApiModelProperty(value = "性别(0-未知、1-男、2-女)")
    private Integer sex;
    /**
     * 城市
     */
    @ApiModelProperty(value = "城市")
    private String city;
    /**
     * 省份
     */
    @ApiModelProperty(value = "省份")
    private String province;
    /**
     * 头像
     */
    @ApiModelProperty(value = "头像")
    private String headimgurl;
    /**
     * 订阅时间
     */
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
    @ApiModelProperty(value = "订阅时间")
    private Date subscribeTime;
    /**
     * 是否关注
     */
    @ApiModelProperty(value = "是否关注")
    @TableField(whereStrategy = FieldStrategy.NEVER)
    private boolean subscribe;
    /**
     * unionid
     */
    @ApiModelProperty(value = "unionid")
    private String unionid;
    /**
     * 备注
     */
    @ApiModelProperty(value = "备注")
    private String remark;
    /**
     * 标签ID列表
     */
    @ApiModelProperty(value = "标签ID列表")
    private JSONArray tagidList;
    /**
     * 关注场景
     */
    @ApiModelProperty(value = "关注场景")
    private String subscribeScene;
    /**
     * 扫码场景值
     */
    @ApiModelProperty(value = "扫码场景值")
    private String qrSceneStr;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    /**
     * 修改时间
     */
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
    @ApiModelProperty(value = "修改时间")
    private Date modifyTime;
    /**
     * 有效状态：0->无效；1->有效
     */
    @TableLogic
    @ApiModelProperty(value = "有效状态：0->无效；1->有效")
    private Integer valid;
    /**
     * 开始时间
     */
    @JsonIgnore
    @TableField(exist = false)
    private String beginTime;

    /**
     * 结束时间
     */
    @JsonIgnore
    @TableField(exist = false)
    private String endTime;

    public WxUser() {
    }

    public WxUser(String openid) {
        this.openid = openid;
    }

    public WxUser(WxMpUser wxMpUser, String appid) {
        this.openid = wxMpUser.getOpenId();
        this.appid = appid;
        this.subscribe = wxMpUser.getSubscribe();
        if (wxMpUser.getSubscribe()) {
            this.nickname = wxMpUser.getNickname();
            this.headimgurl = wxMpUser.getHeadImgUrl();
            this.subscribeTime = new Date(wxMpUser.getSubscribeTime() * 1000);
            this.unionid = wxMpUser.getUnionId();
            this.remark = wxMpUser.getRemark();
            this.tagidList = JSONArray.parseArray(JSONObject.toJSONString(wxMpUser.getTagIds()));
            this.subscribeScene = wxMpUser.getSubscribeScene();
            String qrSceneStr = wxMpUser.getQrSceneStr();
            this.qrSceneStr = !StringUtils.hasText(qrSceneStr) ? wxMpUser.getQrScene() : qrSceneStr;
        }
    }

    public WxUser(WxOAuth2UserInfo wxMpUser, String appid) {
        this.openid = wxMpUser.getOpenid();
        this.appid = appid;
        this.subscribe = wxMpUser.getNickname() != null;
        if (this.subscribe) {
            this.nickname = wxMpUser.getNickname();
            this.headimgurl = wxMpUser.getHeadImgUrl();
            this.unionid = wxMpUser.getUnionId();
        }
    }
}
